package com.xique.door.project.miniprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xique.common.core.utils.DateUtils;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.door.bean.dao.BuildHouseUserDAO;
import com.xique.door.bean.domain.Notice;
import com.xique.door.bean.domain.Problem;
import com.xique.door.bean.domain.User;
import com.xique.door.bean.domain.UserProblem;
import com.xique.door.mapper.BuildAreaMapper;
import com.xique.door.mapper.NoticeMapper;
import com.xique.door.mapper.ProblemMapper;
import com.xique.door.mapper.UserMapper;
import com.xique.door.project.miniprogram.bean.vo.LocationVo;
import com.xique.door.project.miniprogram.bean.vo.MiniNoticeVO;
import com.xique.door.project.miniprogram.bean.vo.MiniProblemVO;
import com.xique.door.project.miniprogram.service.IMiniNoticeService;
import com.xique.door.service.IUserProblemService;
import com.xique.door.service.IUserService;
import com.xique.door.util.DatesUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MiniNoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements IMiniNoticeService {

    @Autowired
    private ProblemMapper problemMapper;

    @Autowired
    private IUserProblemService userProblemService;

    @Autowired
    private IUserService userService;

    @Autowired
    private BuildAreaMapper buildAreaMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult getNoticeList(Long parkId, Integer pageNum, Integer pageSize) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("park_id", parkId);
        queryWrapper.eq("status", 1);
        queryWrapper.ge("release_endtime", new Date());
        queryWrapper.le("release_starttime", new Date());
        queryWrapper.orderByDesc("create_time");
        Page<Notice> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<Notice> list = baseMapper.selectPage(page, queryWrapper);
        long totalPages = list.getPages();
        long totalSize = list.getTotal();
        return PageResult.builder()
                .pageNum(pageNum)
                .pageSize(pageSize)
                .totalPages(totalPages)
                .totalSize(totalSize)
                .list(getMiniNoticeVO(list.getRecords()))
                .build();
    }

    @Override
    public AjaxResult getNoticeList(Long parkId) {
//        User user = new User();
//        user.setBaseUserId(baseUserId);
//        List<User> listuser = userService.selectUserList(user);
//        List<Long> parkId = new ArrayList<>();
//        listuser.forEach(item -> {
//            parkId.add(item.getParkId());
//        });
//        parkId.stream()
//                .distinct()
//                .collect(Collectors.toList());
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("park_id", parkId);
        queryWrapper.eq("status", 1);
        queryWrapper.ge("release_endtime", new Date());
        queryWrapper.le("release_starttime", new Date());
        queryWrapper.orderByDesc("create_time");
        List<Notice> list = baseMapper.selectList(queryWrapper);
        return AjaxResult.success(list);
    }


    @Override
    public AjaxResult getMiniNotice(Long id, Long parkId) {
        User userSearch = new User();
        userSearch.setBaseUserId(SecurityUtils.getBaseUserId());
        userSearch.setParkId(parkId);
        List<User> userList = userMapper.selectUsers(userSearch);
        if (userList != null || userList.size() > 0) {
            User users = userList.get(0);
            Notice notice = baseMapper.selectById(id);
            MiniNoticeVO vo = new MiniNoticeVO();
            if (notice != null) {
                vo.setTitle(notice.getTitle());
                vo.setNoticeType(notice.getNoticeType());
                vo.setViews(notice.getViews());
                vo.setContent(notice.getContent());
                vo.setCreateTime(notice.getCreateTime());
                vo.setNumber(notice.getNumber());
                vo.setReleaseEndtime(notice.getReleaseEndtime());
                if (notice != null && notice.getNoticeType().equals("1")) {
                    vo.setSelectionMethod(notice.getSelectionMethod());
                    QueryWrapper<Problem> wrapper = new QueryWrapper();
                    wrapper.eq("notice_id", notice.getId());
                    List<Problem> list = problemMapper.selectList(wrapper);
                    if (list != null) {
                        notice.setProblemlist(list);
                        List<UserProblem> user = new ArrayList<>();
                        List<MiniProblemVO> voList = new ArrayList<>();
                        for (Problem problem : list) {
                            MiniProblemVO vo1 = new MiniProblemVO();
                            vo1.setOptionContent(problem.getOptionContent());
                            vo1.setNumber(problem.getNumber());
                            vo1.setId(problem.getId());

                            UserProblem userProblem = new UserProblem();
                            userProblem.setParkId(parkId);
                            userProblem.setUserId(users.getId());
                            userProblem.setProblemId(problem.getId());
                            UserProblem userProblem1 = userProblemService.getUserProblemList(userProblem);
                            if (userProblem1 != null) {
                                user.add(userProblem1);
                                vo1.setBool(true);
                            } else {
                                vo1.setBool(false);
                            }
                            voList.add(vo1);
                        }
                        vo.setProblemVOS(voList);
                        if (user.size() > 0) {
                            vo.setBool(true);
                        } else {
                            vo.setBool(false);
                        }
                    }
                }
                notice.setViews(notice.getViews() + 1);
                baseMapper.updateById(notice);
            }
            return AjaxResult.success(vo);
        }
        return AjaxResult.success("该用户没有小区的权限");
    }

    @Override
    public AjaxResult getHorseRaceLampList(Long baseUserId, BigDecimal lng, BigDecimal lat) {

        User user = new User();
        user.setBaseUserId(baseUserId);
        List<User> listuser = userService.selectUserList(user);
        List<Long> parkId = new ArrayList<>();
        if (listuser != null && listuser.size() > 0) {
            listuser.forEach(item -> {
                parkId.add(item.getParkId());
            });

            List<Long> id = new ArrayList<>();
            if (parkId != null && parkId.size() > 0) {
                parkId.stream()
                        .distinct()
                        .collect(Collectors.toList());
                List<LocationVo> vos = buildAreaMapper.seleceteDistance(lng, lat, parkId);
                if (vos != null && vos.size() > 0) {
                    vos.forEach(item -> {
                        id.add(item.getId());
                    });
                }
            }
            QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
            if (id != null && id.size() > 0) {
                queryWrapper.in("park_id", id);

            queryWrapper.eq("play", 1);
            queryWrapper.eq("status", 1);
            // queryWrapper.ge("create_time", DatesUtil.getSevenDate(6));
            queryWrapper.ge("release_endtime", new Date());
            queryWrapper.le("release_starttime", new Date());
            // queryWrapper.orderByDesc("create_time");
            List<Notice> list = baseMapper.selectList(queryWrapper);
            return AjaxResult.success(getMiniNoticeVO(list));
            }
        }
        return AjaxResult.success();
    }

    public List<MiniNoticeVO> getMiniNoticeVO(List<Notice> list) {
        List<MiniNoticeVO> voList = new ArrayList<>();
        if (list.size() > 0) {
            for (Notice notice : list) {
                MiniNoticeVO vo = new MiniNoticeVO();
                vo.setId(notice.getId());
                vo.setTitle(notice.getTitle());
                vo.setNoticeType(notice.getNoticeType());
                vo.setViews(notice.getViews());
                vo.setContent(notice.getContent());
                vo.setCreateTime(notice.getCreateTime());
                vo.setNumber(notice.getNumber());
                voList.add(vo);
            }
        }
        return voList;
    }

}
